package com.bnlr.lrparking.appui.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.appui.stall.StallBegRentBuy;
import com.bnlr.lrparking.appui.stall.StallDealDetailActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.CarPlaceCollectAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.StallListBean;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class SearchStallDealActivity extends BaseActivity {
    private Unbinder unbinder;
    @BindView(R.id.rely_search_back)
    RelativeLayout rlBack;
    @BindView(R.id.lv_search_list)
    RecyclerView recyclerView;
    @BindView(R.id.no_data)
    RelativeLayout empty;
    @BindView(R.id.et_title_search)
    EditText etTitleSearch;
    @BindView(R.id.img_deleteall)
    ImageView ivClose;
    private CarPlaceCollectAdapter adapter;
    private List<StallListBean.DataBean> list=new ArrayList<>();
    private String keyWords = "";
    private int currentPage = 1;
    private String selectID;
    private String type="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_stall_deal);
        unbinder = ButterKnife.bind(this);
        Intent intent = getIntent();
        selectID = AppConfig.getInstance().getString("strCity", "");
        keyWords=intent.getStringExtra("topseach");
        type=intent.getStringExtra("type");
        initView();
    }

    private void initView() {
        etTitleSearch.setText(keyWords);
        etTitleSearch.setOnClickListener(this);
        rlBack.setOnClickListener(this);
        ivClose.setOnClickListener(this);
        getDealData(keyWords,currentPage,type);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter =new CarPlaceCollectAdapter(list);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                //  上拉加载
                currentPage++;
                getDealData(keyWords,currentPage,type);
            }
        },recyclerView);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                StallListBean.DataBean md = list.get(position);
                Intent intent;
                if (md.getType() == 10 || md.getType() == 11) {
                    intent = new Intent(SearchStallDealActivity.this, StallDealDetailActivity.class);
                } else {
                    intent = new Intent(SearchStallDealActivity.this, StallBegRentBuy.class);
                }
                intent.putExtra("stall_id", md.getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.et_title_search:
                finish();
                break;
            case R.id.rely_search_back:
                finish();
                break;
            case R.id.img_deleteall:
                keyWords = "";
                etTitleSearch.setText("");
                currentPage = 1;
                getDealData(keyWords,currentPage,type);
                break;
        }
    }

    /**
     * 获取列表数据
     */
    private void getDealData( String keys, final int page,String type ) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP_SHOW, LrConstant.POST);
            mRequest.add("action", "GetShowInfoTable");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("pageIndex", page);
            mRequest.add("cityName", selectID);
            mRequest.add("type", type);
            if (!AppConfig.getInstance().getString("spaceOwnerType", "").equals("")){
                mRequest.add("spaceOwnerType","2");
            }
            mRequest.add("keyWord", keys);
            mRequest.add("orderBy", 1);
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<StallListBean>(this, true, StallListBean.class) {
                        @Override
                        public void doWork(StallListBean data, String code) {
                            if (TextUtils.equals("1", code)) {
                                if (adapter != null) {
                                    if (data != null && data.getData().size() != 0) {
                                        empty.setVisibility(View.GONE);
                                        if (page == 1) {
                                            list = data.getData();
                                            adapter.setNewData(data.getData());
                                        } else {
                                            list.addAll(data.getData());
                                            adapter.notifyDataSetChanged();
                                        }
                                        adapter.loadMoreComplete();
                                    } else {
                                        if (page == 1) {
                                            list.clear();
                                            adapter.notifyDataSetChanged();
                                            empty.setVisibility(View.VISIBLE);
                                        } else {
                                            adapter.loadMoreEnd();
                                        }
                                    }
                                }
                            } else {
                                ToastUtils.showShort(data.getMsg()+"");
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (!"1".equals(code)) {
                                }
                            } catch(JSONException e){
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
